//
// Created by Administrator on 2021/8/7.
//
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <climits>

using namespace std;

class Solution {
public:
    bool circularArrayLoop(vector<int> &nums) {
        int n = (int) nums.size();
        auto next = [&](int cur) {
            return ((cur + nums[cur]) % n + n) % n;
        };
        for (int i = 0; i < n; ++i) {
            if (!nums[i]) // 遇到0  这条路走不通
                continue;
            int slow = i, fast = next(i);
            // 判断非0且方向相同
            while (nums[slow] * nums[fast] > 0 and nums[slow] * nums[next(fast)] > 0) {
                if (slow == fast) { // 快慢指针相遇
                    if (slow != next(slow)) // 排除不停回到自己的情况
                        return true;
                    else
                        break;
                }
                slow = next(slow);
                fast = next(next(fast));
            }
            int add = i; // 把这条路径全部置为0
            while (nums[add] * nums[next(add)] > 0) {
                int temp = add;
                add = next(add);
                nums[add] = 0;
            }
        }
        return false;
    }
};

int main() {
    vector<int> nums{2, -1, 1, 2, 2};
    Solution solution;
    cout << solution.circularArrayLoop(nums) << endl;
    return 0;
}
